package com.ddx.controller.core;

import com.ddx.common.exception.ResultModel;
import com.ddx.dto.SupplierOrderDto;
import com.ddx.service.IShoppingOrderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author: liyuxiang
 * @create: 2019-01-23
 */
@CrossOrigin(origins = "*", maxAge = 3600)
@RestController
@RequestMapping(value = "/supplier/order")
public class SupplierOrderController {

    private static final Logger logger = LoggerFactory.getLogger(SupplierOrderController.class);

    @Autowired
    IShoppingOrderService orderService;

    /**
     * 订单列表
     * @param request
     * @param supplierOrderDto
     * @return
     */
    @RequestMapping(value = "/getOrderList")
    public ResultModel getOrderList(HttpServletRequest request, @RequestBody SupplierOrderDto supplierOrderDto) {
        try {
            int userId = Integer.parseInt(request.getHeader("userId"));
            return orderService.getOrderLiftForSupplier(userId, supplierOrderDto);
        } catch (Exception e) {
            logger.error("getOrderList error, supplierOrderDto:{}", supplierOrderDto, e);
            return ResultModel.getErrorInstance("服务异常，请稍候重试");
        }
    }

    @RequestMapping(value = "/send")
    public ResultModel send(HttpServletRequest request, @RequestBody Map<String, Object> map) {
        try {
            String orderId = String.valueOf(map.get("orderId"));
            String companyName = String.valueOf(map.get("companyName"));
            String expressNo = String.valueOf(map.get("expressNo"));
            return orderService.send(orderId, companyName, expressNo);
        } catch (Exception e) {
            logger.error("getOrderList error, map:{}", map, e);
            return ResultModel.getErrorInstance("服务异常，请稍候重试");
        }
    }


}
